package com.spica.spicaschool.base;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;
import com.spica.spicaschool.R;
import java.util.List;

/**
 * @ClassName LoadMoreAdapterWrapper
 * @Description 加载更多的封装类
 * @Author Spica2 7
 * @Date 2021/3/14 20:25
 */
public class LoadMoreAdapterWrapper extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

  //底部点击事件
  public interface OnClickRetryListener {
    void onClickRetry();
  }

  private static final int TYPE_LOAD_MORE = Integer.MAX_VALUE - 1;

  @NonNull
  private final RecyclerView.Adapter innerAdapter;
  @Nullable
  private final ILoadMoreAdapter loadMoreListener;
  private final LayoutInflater inflater;
  @Nullable
  private OnClickRetryListener onClickRetryListener;
  private View.OnClickListener onClickListener = new View.OnClickListener() {
    @Override
    public void onClick(View view) {
      if (onClickRetryListener != null) {
        onClickRetryListener.onClickRetry();
      }
    }
  };

  public LoadMoreAdapterWrapper(@NonNull Context context, @NonNull RecyclerView.Adapter<?> adapter, @Nullable ILoadMoreAdapter loadMoreListener) {
    this(context, adapter, loadMoreListener, null);
  }

  public LoadMoreAdapterWrapper(@NonNull Context context, @NonNull RecyclerView.Adapter<?> adapter, @Nullable ILoadMoreAdapter loadMoreListener, @Nullable OnClickRetryListener onClickRetryListener) {
    this.innerAdapter = adapter;
    this.loadMoreListener = loadMoreListener;
    inflater = LayoutInflater.from(context);
    this.onClickRetryListener = onClickRetryListener;
  }

  @NonNull
  public RecyclerView.Adapter<?> getRawAdapter() {
    return innerAdapter;
  }

  @Override
  public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    if (viewType == TYPE_LOAD_MORE) {
      return new LoadMoreItemViewHolder(inflater.inflate(R.layout.list_item_stream_status, parent, false));
    }
    return innerAdapter.onCreateViewHolder(parent, viewType);
  }

  @Override
  public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
    if (holder instanceof LoadMoreItemViewHolder) {
      LoadMoreItemViewHolder h = (LoadMoreItemViewHolder) holder;
      if (loadMoreListener != null && loadMoreListener.hasError()) {
        h.textView.setText("发生错误");
        h.progressBar.setVisibility(View.GONE);
        h.itemView.setOnClickListener(null);
      } else {
        h.textView.setText("点击加载更多");
        h.progressBar.setVisibility(View.GONE);
        h.itemView.setOnClickListener(onClickListener);
      }
    } else {
      innerAdapter.onBindViewHolder(holder, position);
    }
  }

  @Override
  public int getItemCount() {
    return innerAdapter.getItemCount() + (loadMoreListener != null && (loadMoreListener.hasError() || loadMoreListener.hasMoreResults()) ? 1 : 0);
  }

  @Override
  public void onBindViewHolder(RecyclerView.ViewHolder holder, int position, List payloads) {
    if (holder instanceof LoadMoreItemViewHolder) {
      super.onBindViewHolder(holder, position, payloads);
    } else {
      innerAdapter.onBindViewHolder(holder, position, payloads);
    }
  }

  @Override
  public int getItemViewType(int position) {
    if (position >= innerAdapter.getItemCount()) {
      return TYPE_LOAD_MORE;
    }
    return innerAdapter.getItemViewType(position);
  }

  @Override
  public void setHasStableIds(boolean hasStableIds) {
    super.setHasStableIds(hasStableIds);
    innerAdapter.setHasStableIds(hasStableIds);
  }

  @Override
  public long getItemId(int position) {
    if (position >= innerAdapter.getItemCount()) {
      return TYPE_LOAD_MORE;
    }
    return innerAdapter.getItemId(position);
  }

  @Override
  public void onViewRecycled(RecyclerView.ViewHolder holder) {
    if (holder instanceof LoadMoreItemViewHolder) {
      super.onViewRecycled(holder);
    } else {
      innerAdapter.onViewRecycled(holder);
    }
  }

  @Override
  public boolean onFailedToRecycleView(RecyclerView.ViewHolder holder) {
    if (holder instanceof LoadMoreItemViewHolder) {
      return super.onFailedToRecycleView(holder);
    }
    return innerAdapter.onFailedToRecycleView(holder);
  }

  @Override
  public void onViewAttachedToWindow(RecyclerView.ViewHolder holder) {
    if (holder instanceof LoadMoreItemViewHolder) {
      super.onViewAttachedToWindow(holder);
      ViewGroup.LayoutParams params = holder.itemView.getLayoutParams();
      if (params != null && params instanceof StaggeredGridLayoutManager.LayoutParams) {
        ((StaggeredGridLayoutManager.LayoutParams) params).setFullSpan(true);
      }
    } else {
      innerAdapter.onViewAttachedToWindow(holder);
    }
  }

  @Override
  public void onViewDetachedFromWindow(RecyclerView.ViewHolder holder) {
    if (holder instanceof LoadMoreItemViewHolder) {
      super.onViewDetachedFromWindow(holder);
    } else {
      innerAdapter.onViewDetachedFromWindow(holder);
    }
  }

  @Override
  public void registerAdapterDataObserver(RecyclerView.AdapterDataObserver observer) {
    super.registerAdapterDataObserver(observer);
    innerAdapter.registerAdapterDataObserver(observer);
  }

  @Override
  public void unregisterAdapterDataObserver(RecyclerView.AdapterDataObserver observer) {
    super.unregisterAdapterDataObserver(observer);
    innerAdapter.unregisterAdapterDataObserver(observer);
  }

  @Override
  public void onAttachedToRecyclerView(RecyclerView recyclerView) {
    super.onAttachedToRecyclerView(recyclerView);
    innerAdapter.onAttachedToRecyclerView(recyclerView);
    RecyclerView.LayoutManager manager = recyclerView.getLayoutManager();
    if (manager instanceof GridLayoutManager) {
      final GridLayoutManager gridManager = (GridLayoutManager) manager;
      final GridLayoutManager.SpanSizeLookup sizeLookup = gridManager.getSpanSizeLookup();
      gridManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
        @Override
        public int getSpanSize(int position) {
          if (position >= innerAdapter.getItemCount()) {
            return gridManager.getSpanCount();
          }
          return sizeLookup == null ? 1 : sizeLookup.getSpanSize(position);
        }
      });
    }
  }

  @Override
  public void onDetachedFromRecyclerView(RecyclerView recyclerView) {
    super.onDetachedFromRecyclerView(recyclerView);
    innerAdapter.onDetachedFromRecyclerView(recyclerView);
  }

  static class LoadMoreItemViewHolder extends RecyclerView.ViewHolder {
    ProgressBar progressBar;
    TextView textView;

    public LoadMoreItemViewHolder(View itemView) {
      super(itemView);
      progressBar = itemView.findViewById(R.id.progress);
      textView = itemView.findViewById(R.id.text1);
    }
  }
}


